package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type PeopleApi struct {
}

var peopleService = service.ServiceGroupApp.BusinessServiceGroup.PeopleService

// CreatePeople 创建答题人员信息
// @Tags People
// @Summary 创建答题人员信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.People true "创建答题人员信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /people/createPeople [post]
func (peopleApi *PeopleApi) CreatePeople(c *gin.Context) {
	var people business.People
	err := c.ShouldBindJSON(&people)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := peopleService.CreatePeople(&people); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeletePeople 删除答题人员信息
// @Tags People
// @Summary 删除答题人员信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.People true "删除答题人员信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /people/deletePeople [delete]
func (peopleApi *PeopleApi) DeletePeople(c *gin.Context) {
	var people business.People
	err := c.ShouldBindJSON(&people)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := peopleService.DeletePeople(people); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeletePeopleByIds 批量删除答题人员信息
// @Tags People
// @Summary 批量删除答题人员信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除答题人员信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /people/deletePeopleByIds [delete]
func (peopleApi *PeopleApi) DeletePeopleByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := peopleService.DeletePeopleByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdatePeople 更新答题人员信息
// @Tags People
// @Summary 更新答题人员信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body business.People true "更新答题人员信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /people/updatePeople [put]
func (peopleApi *PeopleApi) UpdatePeople(c *gin.Context) {
	var people business.People
	err := c.ShouldBindJSON(&people)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	peopleMap := map[string]interface{}{
		"st": people.St,
	}
	if err := peopleService.UpdatePeople(people.ID, peopleMap); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindPeople 用id查询答题人员信息
// @Tags People
// @Summary 用id查询答题人员信息
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query business.People true "用id查询答题人员信息"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /people/findPeople [get]
func (peopleApi *PeopleApi) FindPeople(c *gin.Context) {
	var people business.People
	err := c.ShouldBindQuery(&people)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if repeople, err := peopleService.GetPeople(people.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"repeople": repeople}, c)
	}
}

// GetPeopleList 分页获取答题人员信息列表
// @Tags People
// @Summary 分页获取答题人员信息列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query businessReq.PeopleSearch true "分页获取答题人员信息列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /people/getPeopleList [get]
func (peopleApi *PeopleApi) GetPeopleList(c *gin.Context) {
	var pageInfo businessReq.PeopleSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := peopleService.GetPeopleInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
